<template>
  <!-- 添加标签弹框 -->
    <el-dialog title="新增目录" :visible="addialogVisible" width="20%"
    @close="btnCancel">
      <!-- 主体区域 -->
      <el-row>
        <el-col>
          <el-form ref="tagsFormRef" :model="tagsForm" :rules="tagsFormRules" label-width="80px" class="demo-ruleForm">
            <el-form-item prop="subjectID" class="elform" v-if="!subjects.id && !subjects.name" label="所属学科" >
              <el-select v-model="tagsForm.subjectID" placeholder="请选择" style="width: 100%;" @focus="addshow">
                <el-option v-for="item in addFromList" :key="item.value" :label="item.label" :value="item.value"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="elform" label="目录名称" prop="tagName">
              <el-input v-model="tagsForm.tagName" placeholder="请输入目录名称" style="width: 100%;"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="addTagsForm">确 定</el-button>
      </span>
    </el-dialog>
</template>

<script>
import { add } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  props: {
    addialogVisible: {
      type: Boolean,
      default: false
    },
    mulu: {
      type: Number,
      default: null
    }
  },
  data () {
    return {
      // 添加标签的表单值
      tagsForm: {
        tagName: '',
        subjectID: ''

      },

      tagsFormRules: {
        tagName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]

      },
      addFromList: []
    }
  },
  computed: {
    subjects () {
      return this.$route.query || null
    }

  },
  methods: {
    async addshow () {
      const { data } = await simple()
      console.log('data55', data)
      this.addFromList = data
    },
    // 添加操作
    addTagsForm () {
      this.$refs.tagsFormRef.validate(async m => {
        if (m) {
          if (this.mulu) {
            console.log(this.mulu)
            await add({
              subjectID: +this.mulu,
              tagName: this.tagsForm.tagName
            })
          } else {
            await add({
              subjectID: +this.tagsForm.subjectID,
              tagName: this.tagsForm.tagName
            })
          }
          this.$message({
            message: '新增成功',
            type: 'success',
            showClose: true,
            duration: 3000
          })
          this.$parent.List()
          this.$emit('update:addialogVisible', false)
        }
      })
    },

    // 取消
    btnCancel () {
      this.$emit('update:addialogVisible', false)
      this.$refs.tagsFormRef.resetFields()
    }
  }
}
</script>

<style scoped lang='less'></style>
